package com.wjk.kylin.modules.system.menu.controller;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.SecurityUtils;
import com.wjk.kylin.modules.system.menu.entity.Menu;
import com.wjk.kylin.modules.system.menu.service.IMenuService;
import com.wjk.kylin.modules.system.role.service.IRoleMenuService;
import com.wjk.kylin.modules.system.vo.TreeSelectVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-01
 */
@Api("菜单")
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends BaseController {

    @Autowired
    private IMenuService menuService;
    @Autowired
    private IRoleMenuService roleMenuService;

    @PreAuthorize("hasAuthority('system:menu:list')")
    @ApiOperation("树形列表")
    @Log(title = "菜单-树形列表",businessType =LIST,isSaveResponseData = false)
    @GetMapping("/treeList")
    public ResponseData treeList() {
        List<Menu> treeList = menuService.treeList();
        return ResponseData.success().data("treeList",treeList);
    }

    @PreAuthorize("hasAuthority('system:menu:query')")
    @ApiOperation("详情")
    @Log(title = "菜单-详情",businessType = GET)
    @GetMapping("/{menuId}")
    public ResponseData get(@PathVariable String menuId) {
        Menu menu = menuService.getById(menuId);
        return ResponseData.success().data("menu", menu);
    }

    @PreAuthorize("hasAuthority('system:menu:add')")
    @ApiOperation("新增")
    @Log(title = "菜单-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody Menu menu) {
        boolean flag = menuService.save(menu);
        if(flag){
            return ResponseData.success();
        }else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:menu:edit')")
    @ApiOperation("编辑")
    @Log(title = "菜单-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody Menu menu) {
        boolean flag = menuService.update(menu);
        if(flag){
            return ResponseData.success();
        }else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:menu:delete')")
    @ApiOperation("删除")
    @Log(title = "菜单-删除",businessType = DELETE)
    @DeleteMapping("/{menuId}")
    public ResponseData delete(@PathVariable String menuId) {
        //todo 系统菜单,不允许删除 id少于8位
        if (StringUtils.isBlank(menuId) || menuId.length() <= 8) {
            return ResponseData.error("系统菜单,不允许删除");
        }

        if (menuService.hasChildByMenuId(menuId)) {
            return ResponseData.error("存在子菜单,不允许删除");
        }

        boolean flag = menuService.deleteById(menuId);
        if(flag){
            return ResponseData.success();
        }else {
            return ResponseData.error("删除失败");
        }
    }


    @ApiOperation("菜单下拉树列表")
    @GetMapping("/treeSelect")
    public ResponseData treeSelect() {
        List<Menu> treeList = menuService.treeList();
        List<TreeSelectVO> treeSelect = treeList.stream().map(TreeSelectVO::new).collect(Collectors.toList());

        return ResponseData.success().data("treeSelect",treeSelect);
    }

    @ApiOperation("加载对应角色菜单列表树")
    @GetMapping(value = "/roleMenuTreeSelect/{roleId}")
    public ResponseData roleMenuTreeSelect(@PathVariable("roleId") String roleId) {
        List<Menu> treeList = menuService.treeList();
        List<TreeSelectVO> treeSelect = treeList.stream().map(TreeSelectVO::new).collect(Collectors.toList());

        //角色拥有的菜单id
        List<String> checkedKeys = roleMenuService.findMenuListByRoleId(roleId);

        return ResponseData.success().data("treeSelect",treeSelect).data("checkedKeys",checkedKeys);
    }


}
